package com.datuo.dtf.admin.service;

import com.datuo.dtf.common.exception.BaseServiceException;
import com.datuo.dtf.mapper.UserMapper;
import com.datuo.dtf.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;


    /**
     * 根据用户名查询
     * @param name
     * @return
     */
    public User findByName(String name){
        User user = new User();
        user.setName(name);
        return userMapper.selectOne(user);
    }

    /**
     * 修改密码
     * @param userId
     * @param password
     */
    public void updatePassword(Long userId,String password){
        User user = new User();
        user.setId(userId);
        user.setPassword(passwordEncoder.encode(password));
        userMapper.updateByPrimaryKeySelective(user);
    }

    /**
     * 查询所有用户
     * @return
     */
    public List<User> queryAllUser(){
        return userMapper.selectAll();
    }
}
